package com.wdd.recoveryvehicle.mapper;

import com.wdd.recoveryvehicle.loginUser.bean.LoginCompanyVB;
import com.wdd.recoveryvehicle.loginUser.bean.LoginPermissionVB;
import com.wdd.recoveryvehicle.loginUser.bean.LoginRolesVB;
import com.wdd.recoveryvehicle.loginUser.bean.LoginUserVB;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * 登录用户
 *
 * @author globalMr
 */
@Mapper
@Repository
public interface LoginUserMapper {

    /**
     * 查询 用户信息 --- 有效
     *
     * @return
     */
    @Select("select " +
            " user_id, user_name, user_nickname, user_phone, " +
            " office_number, company_name, user_email, useful " +
            " from login_user " +
            " where useful = 1 ")
    List<LoginUserVB> selectLoginUserVBSByUseful();

    /**
     * 查询 用户信息 --- 用户名
     *
     * @param user_nickname
     * @return
     */
    @Select("select " +
            " user_id, user_name, user_nickname, user_password, user_phone, " +
            " office_number, company_name, user_email, useful " +
            " from login_user " +
            " where user_nickname = #{user_nickname} and useful = 1 ")
    LoginUserVB selectLoginUserVBByUserNickname(@Param("user_nickname") String user_nickname);

    /**
     * 查询 用户信息 --- 用户ID
     *
     * @param user_id
     * @return
     */
    @Select("select " +
            " user_id, user_name, user_nickname, user_password, user_phone, " +
            " office_number, company_name, user_email, useful " +
            " from login_user " +
            " where user_id = #{user_id} and useful = 1 ")
    LoginUserVB selectLoginUserVBByUserId(@Param("user_id") String user_id);

    /**
     * 查询 用户角色 --- 用户id
     *
     * @param user_id
     * @return
     */
    @Select(" select rore.role_id, role.role_name from user_role_relation rore " +
            " join login_user lour on lour.user_id = rore.user_id " +
            " join user_role role on role.role_id = rore.role_id " +
            " where rore.user_id = #{user_id} and rore.useful = 1 and lour.useful = 1 and role.useful = 1 ")
    Set<LoginRolesVB> selectLoginRoleVBByUserId(@Param("user_id") String user_id);

    /**
     * 查询 用户权限 --- 角色id
     *
     * @param role_id
     * @return
     */
    @Select(" select pere.permission_id, perm.permission_name from user_permission_relation pere " +
            " join user_role role on role.role_id = pere.role_id " +
            " join user_permission perm on perm.permission_id = pere.permission_id " +
            " where pere.role_id = #{role_id} and pere.useful = 1 and role.useful = 1 and perm.useful = 1 ")
    Set<LoginPermissionVB> selelectLoginPermissionVBByRoleId(@Param("role_id") String role_id);

    /**
     * 查询 用户公司 --- user_id
     *
     * @param user_id
     * @return
     */
    @Select(" select core.company_id, usco.company_name from user_company_relation core " +
            " join user_company usco on usco.company_id = core.company_id " +
            " where user_id = #{user_id} and core.useful = 1 and usco.useful = 1 ")
    LoginCompanyVB selectLoginCompanyVBByUserId(@Param("user_id") String user_id);

}
